/**
 @File    :   SSQ_history_data.js
 @Contact :
 @Author  :     ypy

 @Modify Time      @Author    @Version    @Desciption
 ------------      -------    --------    -----------
 2021/2/19 16:38   ypy      1.0           history_data.html对应的js文档，需要在网页文件中加载
 */


/******
 * 页面加载时处理函数
 * *****/
// window.onload = window_onload();//窗体加载后执行

function window_onload() {
    query_year();
    refresh_table_history_data();
}

$(document).ready(window_onload())

/************************
 * 查询指定期号开奖信息
 * **********************************/

//查询按钮单击函数
$("#btn_query_history_by_phasenum").click(function (){
    phase_num = $("#input_search_phasenum").val().trim();//根据文本框值设置全局变量值

    if (phase_num == '') {
        alert('请确定要查询的期号')
        return
    };

    query_one_phasenum_detail_info(phase_num);
});


//指定期号查询
function query_one_phasenum_detail_info(phase_num) {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_history_by_phasenum", phasenum: phase_num,},
        success: function (callback) {
            history_data = callback;
            $('#table_history_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//指定期数查询
function query_multi_phasenum_detail_info(num) {

    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_history_by_multi_phasenum", phasenum: num,},
        success: function (callback) {
            history_data = callback;
            $('#table_history_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })

}


//根据年份查询数据
function  query_history_by_year(year){
     $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_history_by_year", year: year,},
        success: function (callback) {
            history_data = callback;
            $('#table_history_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('查询失败');
        }
    })
}


//select选择后进行查询
$("#select_history_num").change(function () {

    select_num = $("#select_history_num option:selected").text();
    //query_multi_phasenum_detail_info(parseInt(select_num));
    query_history_by_year(select_num)
});


/*************
 * 表格相关函数
 *************/
//刷新表格
function refresh_table_history_data() {
    get_history_data();
    init_table_history_data();

}


//获取历史开奖信息
function get_history_data() {
    //参数为boolean型，如果为true则查询依旧关注股票，false查询停止关注的股票
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "get_history_data"},
        success: function (callback) {
            history_data = callback;
            $('#table_history_data').bootstrapTable("load", history_data);
        },
        error: function () {
            alert('获取历史开奖数据失败，请检查网络连接');
        }
    })
}


//初始化开奖信息表格函数
function init_table_history_data() {

    $('#table_history_data').bootstrapTable("destroy");//  清除原有数据
    $('#table_history_data').bootstrapTable({
        // height: 800,
        locale: "zh-CN",
        columns: [
            {
                title: '期号',
                field: 'phasenum',
                align: 'center',
                valign: 'middle',
                sortable: true,
                // formatter: ts_code_link_Formatter,
            }, {
                title: '日期',
                field: 'date',
                align: 'center',
                valign: 'middle',
                sortable: true,

            }, {
                field: 'red1',
                title: '红球1',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red2',
                title: '红球2',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red3',
                title: '红球3',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red4',
                title: '红球4',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'red5',
                title: '红球5',
                align: 'center',
                valign: 'middle',
                formatter: redball_Formatter,
            }, {
                field: 'blue1',
                title: '蓝球1',
                align: 'center',
                valign: 'middle',
                formatter: blueball_Formatter,
            }, {
                field: 'blue2',
                title: '蓝球2',
                align: 'center',
                valign: 'middle',
                formatter: blueball_Formatter,
            }, {
                field: 'firstnum',
                title: '一等奖注数',
                align: 'center',
                valign: 'middle',
            }, {
                field: 'firstprice',
                title: '一等奖奖金',
                align: 'right',
                valign: 'middle',
            }, {
                field: 'secondnum',
                title: '二等奖注数',
                align: 'center',
                valign: 'middle',
                visible: false,

            }, {
                field: 'secondprice',
                title: '二等奖奖金',
                align: 'right',
                valign: 'middle',
                visible: false,

            }, {
                field: 'totalbet',
                title: '奖池',
                align: 'right',
                valign: 'middle',

            },
            // {
            //     field: 'is_end',
            //     title: '关注',
            //     align: 'center',
            //     formatter: operateFormatter,
            //     // clickToSelect: true,
            // },

        ],
    });
}


//红球显示
function redball_Formatter(value, row, index) {

    var a = '<span style="color: red">' + value + '</span>';

    return a;
}

//蓝球蓝色显示
function blueball_Formatter(value, row, index) {

    var a = '<span style="color: #007bff">' + value + '</span>';

    return a;
}


//添加年份查询选项
function query_year() {
    $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "year"},
        success: function (callback) {
            add_option_to_select_history_num(callback);

        },
        error: function () {

        }
    })
}

//向select中添加年度数据
function add_option_to_select_history_num(result) {
    $("#select_history_num").empty();
    for (i = 0; i < result.length; ++i) {
        var option = "<option  label=\'" + result[i] + "\'  value=\'" + result[i] + "\'  >" +result[i]+ "</option>";//datalist中label时最终显示信息，value是用来查找的，chromn可以同时按两个属性查找
        // var option = "<option value=\'"+result[0][i]+"\'  />";

        $("#select_history_num").append(option);
    }
}


//刷新开奖数据
function  refresh_history_data(){
     $.ajax({
        url: "./",
        type: "GET",
        data: {cmd_class: "query", command: "refresh_history_data", },
        success: function (callback) {
            history_data = callback;
            $('#table_history_data').bootstrapTable("load", history_data);
            alert('已更新最新开奖信息');
        },
        error: function () {
            alert('更新失败');
        }
    })
}